package com.wuhuahe.luanfeng.javabio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created By wuhuahe
 * author:游龙
 * Date: 2018-07-09
 * Time: 19:10
 * Desc:
 */
public class TimeServer {

    public static void main(String[] args) {
        int port = 8080;
        if(args != null && args.length > 0){
            try{
                port = Integer.valueOf(args[0]);
            }catch (NumberFormatException e){}
        }

        ServerSocket serverSocket = null;
        try{
            serverSocket = new ServerSocket(port);
            System.out.println("The time server starts in port:" + port);
            Socket socket = null;
            while (true){
                socket = serverSocket.accept(); // 会阻塞直到有客户端连接过来
                new Thread(new TimeServerHandler(socket)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(serverSocket != null) {
                    System.out.println("The time server close");
                    serverSocket.close();
//                    serverSocket = null;
                }
            } catch (IOException e) {
            }
        }
    }
}
